package com.pshcp.patient.api;

import com.external.hospital.ap.UserApManager;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.common.config.CustomConf;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.thread.ReqComHold;
import com.framework.tool.CommonTool;
import com.pshcp.common.tool.PhotoTools;
import com.pshcp.domain.model.PatientUser;
import com.pshcp.domain.vo.PatientUserVO;
import com.pshcp.patient.domain.patientuser.ao.PatientFeedbackAO;
import com.pshcp.patient.domain.patientuser.ao.PatientUserUpdateAO;
import com.pshcp.patient.tool.PatientUserTools;
import com.pshcp.service.FeedbackService;
import com.pshcp.service.PatientUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = {"患者端-患者账号中心"})
@Slf4j
@RestController
@RequestMapping("/api/patient/patientUser")
public class PatientUserAPI {
    @Autowired
    private CustomConf customConf;

    @Autowired
    private PatientUserService patientUserService;

    @Autowired
    private FeedbackService feedbackService;

    @Autowired
    private UserApManager userApManager;

    @ApiOperation("个人信息")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "个人信息")
    @PostMapping("/information")
    public ResultDTO<PatientUserVO> information() {
        PatientUser patientUser = patientUserService.findById(ReqComHold.getReqCom().getUserId());
        PatientUserVO patientUserVO = CommonTool.copyProperties(patientUser, PatientUserVO.class);
        String fullPath = PhotoTools.getFullPath(customConf.getFileUrl(), patientUserVO.getHeadPhoto());
        patientUserVO.setFullHeadPhotoPath(fullPath);
        return ResultBuilder.success(patientUserVO);
    }

    @ApiOperation("个人信息更新")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.UPDATE, desc = "个人信息更新")
    @PostMapping("/infoUpdate")
    public ResultDTO infoUpdate(@RequestBody PatientUserUpdateAO patientUserUpdateAO) {
        PatientUser patientUser = CommonTool.copyProperties(patientUserUpdateAO, PatientUser.class);
        patientUser.setId(ReqComHold.getReqCom().getUserId());
        patientUserService.update(patientUser);
        //sync to platform
        PatientUser userDb = patientUserService.findById(patientUser.getId());
        userDb.setHeadPhoto(patientUserUpdateAO.getHeadPhoto());
        userDb.setNickName(patientUserUpdateAO.getNickName());
        userDb.setSex(patientUserUpdateAO.getSex());
        userApManager.savePatient(userDb);
        return ResultBuilder.success();
    }

    @ApiOperation("投诉建议")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.ADD, desc = "投诉建议")
    @PostMapping("/suggest")
    public ResultDTO suggest(@RequestBody PatientFeedbackAO feedbackAO) {
        feedbackService.insert(PatientUserTools.buildPatientUserFd(feedbackAO));
        return ResultBuilder.success();
    }

}
